<template>
  <div>
    <div style="display: flex;justify-content: space-between">
      <div>
        <el-input v-model="position.name" placeholder="请输入内容" style="width: 300px"></el-input>
        <el-button type="primary" size="middle" @click="doAddPosition">添加</el-button>
      </div>
      <div>
        <!--  :show-file-list="false" 选择文件后,不展示上传的文件
               :on-success="onSuccess" 上传后端成功后执行的方法
              :on-error="onError" 上传后端失败执行的方法
               :before-upload="beforeupload"  在上传后端前执行的方法.-->
        <el-upload
            action="/system/basic/position/import"
            style="display: inline-flex"
            :show-file-list="false"
            :on-success="onSuccess"
            :on-error="onError"
            :before-upload="beforeUpload">
          <el-button size="mini" type="success" :icon="uploadBtn.icon" :disabled="uploadBtn.disabled">{{ uploadBtn.text }}</el-button>
        </el-upload>
        <el-button size="mini" type="success" icon="el-icon-download" @click="downloadData">导出数据</el-button>
      </div>
    </div>

    <div>
      <!-- :data="positions" 绑定了data()中的position数组,下面各个列通过prop变量从position拿值   -->
      <el-table
          :data="positions"
          style="width: 100%"
          v-loading="loading">
        <!--      prop相当于一个通道,通向:data=position 里拿值.-->
        <el-table-column
            prop="id"
            label="职位编号"
            width="180">
        </el-table-column>
        <el-table-column
            prop="name"
            label="职位名称"
            width="180">
        </el-table-column>
        <el-table-column
            prop="createDate"
            label="创建日期"
            width="280">
        </el-table-column>
        <el-table-column
            label="是否可用">
          <template slot-scope="scope">
            <el-switch
                v-model="scope.row.enabled"
                active-text="启用"
                @change="enableChange(scope.row)"
                inactive-text="禁用">
              <!--              这里要传(scope.row)进去,因为不是点击对话框,scope.row的数据没有拷贝到updateposition中.-->
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
            label="操作">
          <!--   template slot-scope="scope" 固定写法.         -->
          <template slot-scope="scope">
            <!--            打开对话框,将所点击的row对象传进去这个方法.-->
            <el-button size="mini" @click="showUpdatePositionView(scope.row)">编辑</el-button>
            <!--     scope.row表示每一行记录的对象.可以通过scope.row.**  如scope.row.createDate 取值       -->
            <el-button type="danger" size="mini" @click="doDeletePosition(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
<!--         background  页码数字有背景色
            @size-change="handleSizeChange"  选择每页显示记录数时执行的方法.通常时改变data的变量,重写初始化列表
            @current-change="handleCurrentChange" 选择页码时执行的方法.
            :current-page="currentPage" 当前页码
            :page-sizes="[10, 20, 30, 40]" 供用户选择的每页显示记录数,用数组定义就可以.
            :page-size="this.pageSize"  当前页有的记录数.
            layout="total, sizes, prev, pager, next, jumper" 引入组件和组件排序,顺序就按这里前后排序
            :total="this.total" 当前这个查询回来的总记录数 -->
        <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="this.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="this.total">
        </el-pagination>
      </div>
    </div>

    <div>
      <el-dialog
          title="提示"
          :visible.sync="dialogVisible"
          width="30%">
        <table>
          <tr>
            <td>职位名称:</td>
            <td>
              <!--              从data()中拿数据-->
              <el-input size="mini" v-model="updatePosition.name"></el-input>
            </td>
          </tr>
          <tr>
            <td>是否可用</td>
            <td>
              <el-switch
                  v-model="updatePosition.enabled"
                  active-text="启用"
                  inactive-text="禁用">
              </el-switch>
            </td>
          </tr>

        </table>
        <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="doUpdatePosition">确 定</el-button>
  </span>
      </el-dialog>

    </div>
  </div>
</template>

<script>
import {getRequest, putRequest} from "@/util/api";

export default {
  name: "PositionMana",
  data() {
    return {
      //定义一个对象包含多个属性,用于对上传按钮的图标,文字,状态的切换
      uploadBtn:{
        text:'导入数据',
        icon:'el-icon-upload2',
        disabled:false
      },
      //这些变量既作为查询回来显示用,页作为前端请求参数用.
      total:0, //记录总数,预设一个值提供初始化列表时用 ,后面会修改
      currentPage:1, // 当前页码 初始化时用这个参数请求
      pageSize:10,// 每页记录数, 初始化时用这个参数请求
      //定义一个数组,因为查询回来的所有数据都放在里面,一条条记录.
      positions: [],
      loading: false,
      position: {
        name: ''
      },
      dialogVisible: false,
      updatePosition: {
        name: '',
        enabled: null,
      }

    }
  },
  mounted() {

    //================分页方法========================
    //一加载组件就会运行mounted里的各个方法.这里就是加载职位数据
    this.init_positions();
  },
  methods: {
    //官方写法,固定的.newSize是改变每页显示数的值后的值
    handleSizeChange(newSize){
      //把newsize值传给pagesize,
      this.pageSize = newSize;
      //执行初始化方法,就会按照新的pageSize作为参数去初始化
      this.init_positions();
    },
    //官方写法,固定的.newPage是改变页码的值
    handleCurrentChange(newPage){
      //newPage传给currentPage
        this.currentPage= newPage;
      //执行初始化方法,就会按照新的currentPage作为参数去初始化
        this.init_positions();
    },

    //==============文件上传方法================================
    beforeUpload(file){
      this.uploadBtn.text='正在上传';
      this.uploadBtn.icon='el-icon-loading';
      this.uploadBtn.disabled='true';
    },
    onError(response, file, fileList){
      //无论成功失败,按钮还原可操作状态
      this.uploadBtn={
        text:'导入数据',
            icon:'el-icon-upload2',
            disabled:false
      }
      this.$message.error("上传失败")
    },
    onSuccess(response, file, fileList){
      //无论成功失败,按钮还原可操作状态
      this.uploadBtn={
        text:'导入数据',
        icon:'el-icon-upload2',
        disabled:false
      }
      //上传成功是有响应体的.就是参数中的response
      this.$message.success(response.msg)
    },
    //=======================文件下载===============================
    downloadData() {
      window.open("/system/basic/position/download", "_blank")
    },
    enableChange(data) {
      this.putRequest("/system/basic/position/", data).then((resp) => {
        if (resp) { // 如果成功了,resp才又返回值,才进行里面的操作
          this.init_positions(); //初始化表格
        }

      })
    },
    //=====================编辑============================
    doUpdatePosition() {
      this.putRequest("/system/basic/position/", this.updatePosition).then((resp) => {
        if (resp) { // 如果成功了,resp才又返回值,才进行里面的操作
          this.dialogVisible = false;  //对话框关闭
          this.init_positions(); //初始化表格
        }

      })
    },
    showUpdatePositionView(data) {
      this.dialogVisible = true;
      //这样就会连同引用一起拷贝过去, updatePosition就会指向scope.row. updatePosition的值改变.scope.row的值就跟着改变
      //this.updatePosition = data
      //所以其实是需要拷贝具体数据,不是要拷贝地址引用
      Object.assign(this.updatePosition, data)
    },
    //========================初始化列表==============================
    init_positions() {
      this.loading = true;
      this.getRequest("/system/basic/position/page?page="+ this.currentPage + "&size="+ this.pageSize).then(resp => {
        this.loading = false;
        if (resp) {
          this.positions = resp.data; //后端改用Respbean返回,所以列表数据在resp的data属性里.
          this.total = resp.total; //发挥的resp有total属性
        }
      })
    },
    //=============================添加===============================
    doAddPosition() {
      this.postRequest("/system/basic/position/", this.position).then((resp) => {
        if (resp) {
          this.position.name = '';
          this.init_positions();
        }

      })
    },
    //==============================删除=============================
    doDeletePosition(data) {
      this.$confirm('此操作将永久删除该职位' + data.name + ', 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest("/system/basic/position/" + data.id).then((resp) => {
          if (resp) {
            this.init_positions();
          }
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  }
}
</script>

<style scoped>

</style>